package com.rocksdata.mc.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rocksdata.mc.R;
import com.rocksdata.mc.entity.NewsInfo2;

import java.util.List;

/**
 * Created by 陈堂正 on 2017/5/12.
 */

public class WidgetNewsRecyclerAdapter extends RecyclerView.Adapter<AbsNewsHolder> {
    private static final int TYPE_SINGLE_BIG_IMAGE = 1;
    private static final int TYPE_SINGLE_SMALL_IMAGE = 2;
    private static final int TYPE_MULTI_IMAGE = 3;
    private static final int TYPE_NO_IMAGE = 4;

    private List<NewsInfo2> newsInfoList;
    private LayoutInflater inflater;
    private OnItemClickListener listener;

    public WidgetNewsRecyclerAdapter(Context context, List<NewsInfo2> newsInfoList){
        this.newsInfoList = newsInfoList;
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @SuppressWarnings("ConstantConditions")
    @Override
    public AbsNewsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_SINGLE_BIG_IMAGE:
                return new NewsSingleBigImageHolder(inflater.inflate(R.layout.item_news_big_image, parent, false));
            case TYPE_SINGLE_SMALL_IMAGE:
                return new NewsSingleSmallImageHolder(inflater.inflate(R.layout.item_news_small_image, parent, false));
            case TYPE_MULTI_IMAGE:
                return new NewsMultiImageHolder(inflater.inflate(R.layout.item_news_multi_image, parent, false));
            case TYPE_NO_IMAGE:
                return new NewsNoImageHolder(inflater.inflate(R.layout.item_news_no_image, parent, false));
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onBindViewHolder(@NonNull AbsNewsHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.bindData(newsInfoList.get(position));
        if(listener != null){
            holder.getItemView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(newsInfoList.get(position));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return newsInfoList.size();
    }

    @Override
    public int getItemViewType(int position) {
        NewsInfo2 info = newsInfoList.get(position);
//        if(position == 0 && info.getImgURL() != null && info.getImgURL().size() == 1){
//            return TYPE_SINGLE_BIG_IMAGE;
//        }else{
            if(info.getImgURL() == null || info.getImgURL().size() == 0) {
                return TYPE_NO_IMAGE;
            }else if(info.getImgURL().size() == 1){
                return TYPE_SINGLE_SMALL_IMAGE;
            }else{
                return TYPE_MULTI_IMAGE;
            }
//        }
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

    public interface OnItemClickListener{
        void onItemClick(Object item);
    }
}
